﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Expo.Training.DataAccess.Course;
using Expo.Training.Entity;
using System.Net;
using System.Text;
using System.Web.Script.Serialization;
using Expo.Training.DataAccess.Special;
using Expo.Training.BRule.Ad;

public partial class BM_Default : Pagebase
{
    public int IsLogin=0;

    //是否显示专项推送的More
    public bool IsShowSpecialMore = true;

    protected void Page_Load(object sender, EventArgs e)
    {
        //判断是否登陆
        if (this.Member.MemberID == null || this.Member.MemberID == Guid.Empty)
        {

            IsLogin = 0;

            //HiddenIsLogin.Value = IsLogin.ToString();

        }
        else
        {
            IsLogin = 1;

            //HiddenIsLogin.Value = IsLogin.ToString();
        }

        

        if (!Page.IsPostBack)
        {
            BindNews();
            BindLocal();
            BindCourseList();
            BindCatalogList();
            QuestionnaireListBind();
            //绑定友情链接
            BindFrindLinks();
            //绑定专项推送列表
            BindXBSpecialList();
            //BindBMSpecialList();
            //TransBank();
            Advertisement ad = new Advertisement();
            DataTable dt = ad.QueryDataTable();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (Convert.ToInt32(dt.Rows[i]["vesion"]) == 0
                    && Convert.ToInt32(dt.Rows[i]["IsVisble"])==0)
                { 
                    this.firstAd.Visible=false;
                }

                if (Convert.ToInt32(dt.Rows[i]["vesion"]) == 4
                    && Convert.ToInt32(dt.Rows[i]["IsVisble"]) == 0)
                {
                    this.lastAd.Visible = false;
                }
            }

        }

        //lblToday.Text = Application["counter"].ToString();
        Online_Member online = new Online_Member();
        online.OnlineID = "95000";
        if (online.Exist())
        {
            this.lblTotal.Text = (online.TotalOnline+700000).ToString();
            this.lblTotalToday.Text = online.TotalTodayOnline.ToString();
        }
        
    }

    public void BindBMSpecialList()
    {
        //DataTable dt = SpecialManager.GetSpecialListForBM("IsTop Desc,Number ASC ,AddTime DESC");
        //Rpt_BM_SpecialList.DataSource = dt;
        //Rpt_BM_SpecialList.DataBind();
    }

    public void BindXBSpecialList()
    {
        DataTable dt = SpecialManager.GetSpecialList("IsTop Desc,Number ASC ,AddTime DESC");
        Rpt_XB_SpecialList.DataSource = dt;
        Rpt_XB_SpecialList.DataBind();
    }


    /// <summary>
    /// 绑定目录第一层
    /// </summary>
    public void BindCatalogList()
    {
        //Modify By Yinqi 2014-07-23
        //修改Course_CatalogV2表为Resource_Catalog表
        //Course_CatalogV2 catalog = new Course_CatalogV2();
        Resource_Catalog catalog = new Resource_Catalog();
        catalog.ParentID = Guid.Empty;
        catalog.OrderBy = " CatalogNo ";
        catalog.CustomCondition = " CustomerID<>'00000000-0000-0000-0000-000000000000'";
        DataTable dt = catalog.QueryDataTable();
        rpt_CatalogLevelOne.DataSource = dt;
        rpt_CatalogLevelOne.DataBind();
    }

    /// <summary>
    /// 绑定资源列表
    /// </summary>
    public void BindCourseList()
    {
        BindCourseListByCommend();
        BindCourseListByQuantity();
        BindCourseListByComment();
        BindCourseListByAddTime();
    }

    /// <summary>
    /// 按推荐绑定数据
    /// </summary>
    public void BindCourseListByCommend()
    {
        DataTable dt = Course.GetCourseListByCommend();
        rpt_CourseListCommend.DataSource = dt;
        rpt_CourseListCommend.DataBind();
    }

    /// <summary>
    /// 按人数绑定数据
    /// </summary>
    public void BindCourseListByQuantity()
    {
        DataTable dt = Course.GetCourseListByQuantity();
        rpt_CourseListQuantity.DataSource = dt;
        rpt_CourseListQuantity.DataBind();
    }

    /// <summary>
    /// 按评论绑定数据
    /// </summary>
    public void BindCourseListByComment()
    {
        DataTable dt = Course.GetCourseListByComment();
        rpt_CourseListComment.DataSource = dt;
        rpt_CourseListComment.DataBind();
    }

    /// <summary>
    /// 按时间绑定数据
    /// </summary>
    public void BindCourseListByAddTime()
    {
        DataTable dt = Course.GetCourseListByAddTime();
        rpt_CourseListAddTime.DataSource = dt;
        rpt_CourseListAddTime.DataBind();
    }

    public void BindLocal()
    {
        Learning_Place place = new Learning_Place();
        place.ReturnCount = 4;
        place.LearningType = 1;
        place.ParentName_LIKE = "巴马";
        DataTable dt = place.QueryDataTable();
        this.rptLocal.DataSource = dt;
        this.rptLocal.DataBind();
    }

    /// <summary>
    /// 截断标题名为20位
    /// </summary>
    /// <param name="Name">标题名</param>
    /// <returns>20位标题名</returns>
    public string GetTitleName(string Name)
    {
        string TitleName="";

        if(Name.Length<20)
        {
            TitleName=Name;
        }
        else
        {
           TitleName= Name.Substring(0,17)+"...";
        }

        return TitleName;
    }

    protected void BindNews()
    {
        News news = new News();
        news.Type = 55;
        news.ReturnCount = 6;
        news.IsPass = 1;
        //Add  By Yinqi 2016-03-24
        //增加下线时间的筛选
        news.CustomCondition = "(IsTop=0 or (IsTop=1 and TopExpiredDate >'" + DateTime.Now + "' ))";
        news.OrderBy = " istop desc,AddTime desc";
        DataTable dt = news.QueryDataTable();
        rptNewsFresh.DataSource = dt;
        rptNewsFresh.DataBind();
    }

    protected string GetSnsURL()
    {
        return System.Configuration.ConfigurationManager.AppSettings["BlogSNSUrl"];
    }

    protected void TransBank()
    {
        this.lbWeather.Text = string.Empty;
        WebClient wc = new WebClient();
        byte[] json = wc.DownloadData(new Uri("http://m.weather.com.cn/data/101210401.html", UriKind.Absolute));
        string recData = Encoding.GetEncoding("UTF-8").GetString(json);
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        Dictionary<string, object> diJson = (Dictionary<string, object>)serializer.DeserializeObject(recData);
        foreach (KeyValuePair<string, object> str in diJson)
        {
            Dictionary<string, object> dv = (Dictionary<string, object>)str.Value;
            foreach (KeyValuePair<string, object> value in dv)
            {
                if (value.Key.Equals("city"))
                {
                    this.lbWeather.Text += "城市:慈溪&nbsp&nbsp";
                }

                if (value.Key.Contains("temp1"))
                {
                    this.lbWeather.Text += value.Value + "~";
                }

                if (value.Key.Contains("temp2"))
                {
                    this.lbWeather.Text += value.Value + "&nbsp&nbsp";
                }

                if (value.Key.Contains("weather"))
                {
                    this.lbWeather.Text += value.Value + "&nbsp&nbsp";
                }
            }
        }
    }

    private void QuestionnaireListBind()
    {
        List<Questionnaire_Info> objList = Expo.Training.DataAccess.Portal.Questionnaire.QuestionnaireModule.GetQuestionnaire(2, 0, 1, 4, 1, DateTime.Now, Customer.CustomerID);
        rep_QuestionnaireList.DataSource = objList;
        rep_QuestionnaireList.DataBind();
    }

    #region 绑定友情链接
    private void BindFrindLinks()
    {
        BindRptAds();
        BindOtherFriendLinks();

        ListItem li = new ListItem("::区内网站链接::", "-1");
        ddlDirect.Items.Insert(0, li);

        ListItem li2 = new ListItem("::常用网站链接::", "-1");
        ddlCommon.Items.Insert(0, li2);

        ListItem li3 = new ListItem("::相关网站链接::", "-1");
        ddlRelation.Items.Insert(0, li3);
    }

    private void BindRptAds()
    {
        string title = string.Empty;// this.txt_title.Text;
        DateTime beginDate = DateTime.Parse("1900-1-1");
        DateTime endDate = DateTime.Parse("1900-1-1");

        DataTable objTable = GetPicAdsList(title, beginDate, endDate);
        if (objTable == null)
        {
            return;
        }
        this.rpt_Ads.DataSource = objTable.DefaultView;
        this.rpt_Ads.DataBind();
    }

    private DataTable GetPicAdsList(string title, DateTime beginDate, DateTime endDate)
    {
        int totalCount = 0;
        DataTable dt = AdsBLL.GetAdsList(1, 10, out totalCount, "CreateDate DESC", title, beginDate, endDate, 1, -1, 4);
        return dt;
    }

    private void BindOtherFriendLinks()
    {
        int totalCount = 0;
        DateTime beginDate = DateTime.Parse("1900-1-1");
        DateTime endDate = DateTime.Parse("1900-1-1");
        DataTable dt = AdsBLL.GetAdsList(1, 65535, out totalCount, "CreateDate DESC", "", beginDate, endDate, 1, -1, 5);
        ddlDirect.DataSource = dt;
        ddlDirect.DataTextField = "Title";
        ddlDirect.DataValueField = "Url";
        ddlDirect.DataBind();

        dt = AdsBLL.GetAdsList(1, 65535, out totalCount, "CreateDate DESC", "", beginDate, endDate, 1, -1, 6);
        ddlCommon.DataSource = dt;
        ddlCommon.DataTextField = "Title";
        ddlCommon.DataValueField = "Url";
        ddlCommon.DataBind();

        dt = AdsBLL.GetAdsList(1, 65535, out totalCount, "CreateDate DESC", "", beginDate, endDate, 1, -1, 7);
        ddlRelation.DataSource = dt;
        ddlRelation.DataTextField = "Title";
        ddlRelation.DataValueField = "Url";
        ddlRelation.DataBind();

    }
    #endregion

    public string getQuestionnaireName(string QuestionnaireName, string QuestionnaireType)
    {
        if (QuestionnaireType == "1" || QuestionnaireType == "2")
        {
            QuestionnaireName += string.Format("({0})", QuestionnaireType == "1"?"问卷":"调查");
        }
        return QuestionnaireName;
    }
}